/*
 *
 *  Copyright (C) 2008 LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */

package com.LibreGeoSocial.App;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;

import com.LibreGeoSocial.Api.GeoNode;
import com.LibreGeoSocial.App.Overlay.MapOverlay;
import com.LibreGeoSocial.App.Overlay.RoutePathOverlay;
import com.LibreGeoSocial.App.Routes.GoogleRoutes;
import com.LibreGeoSocial.App.Routes.GoogleRoutesList;
import com.LibreGeoSocial.App.Routes.LocationService;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;



public class ViewMap extends MapActivity {

	   private MapView mapView;
	   
	   private GeoPoint point;
	   
	   private String mUrl;

	   private static final int MENU_MAP = Menu.FIRST + 1;
	   private static final int MENU_MAP_SATELITE = Menu.FIRST + 2;
	   private static final int MENU_MAP_NORMAL = Menu.FIRST + 3;
	   private static final int MENU_GOTOPLACE = Menu.FIRST + 4;
	   private static final int MENU_GOTOURL = Menu.FIRST + 5;
	   
	   private Double mLatitude, mLongitude;
	   @Override
	   public void onCreate(Bundle icicle) {
		   
		   super.onCreate(icicle);
		   
		   FrameLayout frame = new FrameLayout(this);
		   
		   mapView = new MapView(this, getString(R.string.map_key));
		   MapController mapControl = mapView.getController();
		   
		  
	       frame.addView(mapView, 
	                new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

	       
		   setContentView(frame);		  
		   
		   /* Obtain data */
		   Intent i = getIntent();

		   mLatitude  = i.getDoubleExtra("latitude", Integer.MIN_VALUE);
		   mLongitude = i.getDoubleExtra("longitude", Integer.MIN_VALUE);
		   mUrl 			= i.getStringExtra("url");

		   point = new GeoPoint((int) (mLatitude * 1000000), (int) (mLongitude * 1000000));
		   mapControl.setZoom(20);
		   mapControl.setCenter(point);
		   
		   /* Config overlay */
		   MapOverlay myMapOver = new MapOverlay();
		   myMapOver.setDrawable(getResources().getDrawable(R.drawable.map_pin));
		   myMapOver.setGeoPoint(point);
		   
		   
	       final List<Overlay> overlays = mapView.getOverlays();
	       overlays.add(myMapOver);
	       
	       /* Settings map */
	       mapView.setClickable(true);
	       mapView.setEnabled(true);
	       mapView.setSatellite(false);

		   addZoomControls(frame);

	   }
	   
	   @Override
	   protected boolean isRouteDisplayed() {
	      // TODO Auto-generated method stub
	      return false;
	   }

	   
	   /**
	     * Get the zoom controls and add them to the bottom of the map
	     */
	    private void addZoomControls(FrameLayout frame) {
	        View zoomControls = mapView.getZoomControls();

	        FrameLayout.LayoutParams p = 
	            new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
	                LayoutParams.WRAP_CONTENT, Gravity.BOTTOM + Gravity.CENTER_HORIZONTAL);
	        frame.addView(zoomControls, p);
	    }
	    

	    
	    @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	    	
	        super.onCreateOptionsMenu(menu);
	         
	        SubMenu sub = menu.addSubMenu(0,MENU_MAP,0,"View Map");
	        
	        sub.add(0, MENU_MAP_SATELITE, 0, "Satelite View");
	        
	        sub.add(0, MENU_MAP_NORMAL, 0, "Normal View");
   
	        menu.add(0, MENU_GOTOPLACE, 0, "Go to Place")
    					.setAlphabeticShortcut('C');
	        
	        if (mUrl != "")
	        {
	        	menu.add(0, MENU_GOTOURL, 0, "Go to Url")
	        	.setAlphabeticShortcut('C');
	        }
	        
	        return true;
	    }
	    
	    @Override
	    public boolean onOptionsItemSelected (MenuItem item) {
	    	
	    	switch (item.getItemId()) {
	    	
	    	case MENU_MAP_SATELITE:
	    		mapView.setSatellite(true);
	    		break;
	    		
	    	case MENU_MAP_NORMAL:
	    		mapView.setSatellite(false);
	    		break;
	    		
	    	case MENU_GOTOPLACE:
	    		
	    		Intent i2 = new Intent(this,GoogleRoutesList.class);

	    		i2.putExtra("latitude", mLatitude);
	    		i2.putExtra("longitude", mLongitude);
	    		
	        	startActivity(i2);  
	        	
	        	/* TODO: This code is repeat. 
	        	 *       The previous intent must return the routePath */
	        	
	            GoogleRoutes myRoute = new GoogleRoutes();
	            
	            myRoute.setSource( LocationService.getCurrentLocation().getLatitude(), 
	            				   LocationService.getCurrentLocation().getLongitude());
          
	            myRoute.setDestination( mLatitude, mLongitude);
     
	            myRoute.setLanguage("es");
	            
	            myRoute.getRoute();
	            ArrayList<GeoNode> routePath = myRoute.getRoutePath();
	            
	            
	        	mapView.getOverlays().clear();

	    		final List<Overlay> overlays = mapView.getOverlays();
	        	    	
	        	RoutePathOverlay  myRoytePathOverlay = new RoutePathOverlay();
	        	myRoytePathOverlay.setRoutePath (routePath);
	        	
	        	overlays.add(myRoytePathOverlay);
	        	
	        	mapView.invalidate();
	            
	    		/*
	    		List<Address> adresses = null;
	    		
	    		Geocoder gc = new Geocoder (this, Locale.getDefault());
	    		try {
						adresses = gc.getFromLocation(-3.867252, 40.335537, 10);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	    		
					Address address = adresses.get(0);
					
					for (int i=0; i< address.getMaxAddressLineIndex(); i++)
					{
						Log.e("GPS", address.getAddressLine(i) + "\n");
						Log.e("GPS", address.getLocality() + "\n");
						Log.e("GPS", address.getPostalCode() + "\n");
						Log.e("GPS", address.getCountryName() + "\n");
						Log.e("GPS", "===========================\n");
					}
	    		*/
	    		
	    		break;
	    	case MENU_GOTOURL:
	    		
	            Intent i = new Intent(Intent.ACTION_VIEW);
	            i.setData(Uri.parse(mUrl));
	            startActivity(i);

	    		break;
	        }
	        return super.onOptionsItemSelected(item);
	    }
	    
	    
	    protected void onActivityResult (int requestCode, int resultCode, Intent data) { 
	    
	    }


	}